package servlet;

import entity.User;
import jdk.management.resource.internal.inst.SocketOutputStreamRMHooks;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request-----------------------------------------------
        request.setCharacterEncoding("UTF-8");
        //1.存储字符串类型数据：
        request.setAttribute("username", "伍鑫鑫");
        //2.request中存储String集合数据：
        List<String> names = new ArrayList<>();
        names.add("伍鑫鑫");
        names.add("杨大石");
        names.add("黄凯");
        names.add("邓妍");
        //String s = names.get(0);//获取集合中指定索引位置的元素
        request.setAttribute("names", names);

        //3.request中存储List<User>集合数据：
        List<User> users = new ArrayList<>();
        users.add(new User(1001, "小美", "xiaoMei", '女'));
        users.add(new User(1002, "小帅", "xiaoShuai", '男'));
        users.add(new User(1003, "大山", "daShan", '男'));
        users.add(new User(1004, "大壮", "daZhuang", '男'));
        request.setAttribute("users", users);

        //4.request中存储Map<String,Object>集合数据：
        Map<String, Object> maps = new HashMap<>();
        maps.put("ABC", "中国农业银行");
        maps.put("CBC", "中国建设银行");
        maps.put("ICBC", "中国工商银行");
        request.setAttribute("maps", maps);


        //session-----------------------------------------------

        //servlet中实例化session
        HttpSession session = request.getSession();
        session.setAttribute("loginName", "王大锤子");
        User user = new User(10086, "奥德飙", "aoDeBiao", '男');
        session.setAttribute("loginUser", user);
        //跳转到首页
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
}
